package com.liu.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.liu.controller.GenericServlet;
import com.liu.dao.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author 老刘Matthew
 * @Timestamp 2022/7/14 22:23
 * @Description
 */
public interface HttpServletFactory extends LoggerFactory {

    IUserDao userDao = DaoFactory.getUserDao();

    INovelDao novelDao = DaoFactory.getNoVelDao();

    IReadLogDao readLogDao = DaoFactory.getReadLogDao();

    IFavoriteDao favoriteDao = DaoFactory.getFavoriteDao();

    IRateDao rateDao = DaoFactory.getRateDao();

    IChapterDao chapterDao = DaoFactory.getChapterDao();

    Gson gson = new GsonBuilder().create();

    default String toJsp(String url) {

        return "/jsp/" + url + ".jsp";
    }

    default String upload(HttpServletRequest request, String subPath) throws ServletException, IOException {

        // Windows 需要修改路径
        final String FILE_UPLOAD_PATH = request.getServletContext().getRealPath("/upload" + subPath + "/");

        Part filePart = request.getPart("image");
        String fileName = System.currentTimeMillis() + "_" + filePart.getSubmittedFileName();

        if (null == fileName || fileName.isEmpty())
            request.setAttribute("msg", "修改头像失败，请检查上传文件！");
        else {

            for (Part part : request.getParts())
                part.write(FILE_UPLOAD_PATH + fileName);

            request.setAttribute("msg", "修改头像成功！");
        }

        return fileName;
    }

}



